#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

char line[100];

const int KEYWORD = 0;
const int SIGN = 1;
const int NNN = 2;

const int NAVI = 3;
const int TIME_KEEP = 4;
const int DIRECTIONAL = 5;
const int HOW = 6;
const int DIRECTION = 7;
const int WHERE = 8;
const int WHEN = 9;
// const int CHANGE = 10;
const int INSTR = 11;
const int UNKNOWN = 12;

const int GO = 13;
const int AND = 14;
const int THEN = 15;
const int KEEP = 16;
const int RIGHT = 17;
const int LEFT = 18;
const int FIRST = 19;
const int SECOND = 20;
const int THIRD = 21;
const int AT = 22;
const int RECORD = 23;
const int TIME = 24;
const int TO = 25;
const int KMH = 26;
const int CHANGE = 27;
const int AVERAGE = 28;
const int SPEED = 29;
const int CAS = 30;

const int FG = 31;
const int ANDTHEND = 32;
const int THEND = 33;
const int TONNNKMH = 34;
const int NNNKMH = 35;
const int ANDC = 36;
const int AVERAGESPEED = 37;

typedef struct TToken {
	int type;
	string val;
} TToken;

const int S_INIT = 0, S_KEYWORD = 1, S_SIGN = 2, S_NNN = 3, S_END = 4;

typedef vector<TToken> TCell;
typedef vector<TCell> TRow;

TCell bigTable[100][100];
bool caled[100][100];
vector<TToken> globalList;
vector<TToken> originalList;

bool operator < (const TToken& x, const TToken& y){return x.type < y.type;}
bool operator == (const TToken& x, const TToken& y){return x.type == y.type;}
bool isCh(char x) {return 'A'<=x && x<='Z';}
bool isLetter(char x) {return x=='.'||isCh(x);}
ostream& operator << (ostream& out, const TCell& cell) {
	out << "cell: ";
	for (int i=0; i<cell.size(); ++i)
		out << cell[i].type << " ";
	return out;
}

bool simplify(vector<TToken>& tokenlist) {
	for (int i=0; i<tokenlist.size(); ++i) {
		if (tokenlist[i].type == KEYWORD) {
			if (tokenlist[i].val == "AND")
				tokenlist[i].type = AND;
			else if (tokenlist[i].val == "THEN")
				tokenlist[i].type = THEN;
			else if (tokenlist[i].val == "GO")
				tokenlist[i].type = HOW;
			else if (tokenlist[i].val == "KEEP")
				tokenlist[i].type = HOW;
			else if (tokenlist[i].val == "RIGHT")
				tokenlist[i].type = DIRECTION;
			else if (tokenlist[i].val == "LEFT")
				tokenlist[i].type = DIRECTION;
			else if (tokenlist[i].val == "FIRST")
				tokenlist[i].type = WHEN;
			else if (tokenlist[i].val == "SECOND")
				tokenlist[i].type = WHEN;
			else if (tokenlist[i].val == "THIRD")
				tokenlist[i].type = WHEN;
			else if (tokenlist[i].val == "AT")
				tokenlist[i].type = AT;
			else if (tokenlist[i].val == "RECORD")
				tokenlist[i].type = RECORD;
			else if (tokenlist[i].val == "TIME")
				tokenlist[i].type = TIME;
			else if (tokenlist[i].val == "KMH")
				tokenlist[i].type = KMH;
			else if (tokenlist[i].val == "CHANGE")
				tokenlist[i].type = CHANGE;
			else if (tokenlist[i].val == "AVERAGE")
				tokenlist[i].type = AVERAGE;
			else if (tokenlist[i].val == "SPEED")
				tokenlist[i].type = SPEED;
			else if (tokenlist[i].val == "CAS")
				tokenlist[i].type = CAS;
			else if (tokenlist[i].val == "TO")
				tokenlist[i].type = TO;
			else {
				return false;
			}
		}
	}
	
	return true;
}

inline TToken ttoken(int v) {TToken t;t.type=v;return t;}

vector<TToken> findSym(const vector<TToken>& list1, const vector<TToken>& list2) {
	vector<TToken> res;
	for (int i=0; i<list1.size(); ++i) {
		switch (list1[i].type) {
		case HOW:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == DIRECTION || list2[j].type == FG) {
					res.push_back(ttoken(INSTR));
					res.push_back(ttoken(NAVI));
					res.push_back(ttoken(DIRECTIONAL));
				}
				else if (list2[j].type == WHEN) {
					res.push_back(ttoken(HOW));
				}
			break;
		case DIRECTION:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == WHERE) {
					res.push_back(ttoken(FG));
				}
			break;
		case NAVI:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == ANDTHEND) {
					res.push_back(ttoken(INSTR));
					res.push_back(ttoken(NAVI));
				}
				else if (list2[j].type == ANDC)
					res.push_back(ttoken(INSTR));
			break;
		case AND:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == THEND)
					res.push_back(ttoken(ANDTHEND));
				else if (list2[j].type == TIME_KEEP)
					res.push_back(ttoken(ANDC));
			break;
		case THEN:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == DIRECTIONAL)
					res.push_back(ttoken(THEND));
			break;
		case RECORD:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == TIME) {
					res.push_back(ttoken(INSTR));
					res.push_back(ttoken(TIME_KEEP));
				}
			break;
		case CAS:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == TONNNKMH) {
					res.push_back(ttoken(TIME_KEEP));
					res.push_back(ttoken(INSTR));
				}
			break;
		case TO:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == NNNKMH)
					res.push_back(ttoken(TONNNKMH));
			break;
		case NNN:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == KMH)
					res.push_back(ttoken(NNNKMH));
			break;
		case AT:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == SIGN)
					res.push_back(ttoken(WHERE));
			break;
		case CHANGE:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == AVERAGESPEED)
					res.push_back(ttoken(CAS));
			break;
		case AVERAGE:
			for (int j=0; j<list2.size(); ++j)
				if (list2[j].type == SPEED)
					res.push_back(ttoken(AVERAGESPEED));
			break;
		}
	}

	return res;
}

TCell search(int s, int t) {
	if (caled[s][t]) {
		return bigTable[s][t];
	}
	TCell cell;
	if (t<=s){
// 		cout << "error" << endl;
// 		while (true);
		return cell;
	}
	if (t-s == 1) {
		cell.push_back(globalList[s]);
		caled[s][t] = true;
		bigTable[s][t] = cell;
		return cell;
	}
	for  (int i=s+1; i<t; ++i) {
		TCell left = search(s, i);
		TCell right = search(i, t);
		TCell sym = findSym(left, right);
		for (int i=0; i<sym.size(); ++i)
			cell.push_back(sym[i]);
	}
	caled[s][t] = true;
	sort(cell.begin(), cell.end());
	TCell::iterator it = unique(cell.begin(), cell.end());
	cell.resize(it - cell.begin());
	bigTable[s][t] = cell;
// 	cout << "in search: " << s << " " << t << endl;
// 	cout << cell << endl;
	return cell;
}

bool eval(vector<TToken> tokenlist) {
	if (!simplify(tokenlist)) return false;
	TRow row;
	for (int i=0; i<tokenlist.size(); ++i) {
		vector<TToken> tlist;
		tlist.push_back(tokenlist[i]);
		row.push_back(tlist);
	}
	
	memset(caled, 0, 100*100);
	globalList = tokenlist;
	search(0, tokenlist.size());
	
// 	for (int i=0; i<tokenlist.size(); ++i) {
// 		for (int j=i+1; j<=tokenlist.size(); ++j) {
// 			TCell cell = bigTable[i][j];
// 			printf("cell[%d][%d]: \n", i, j);
// 			for (int k=0; k<cell.size(); ++k)
// 				cout << cell[k].type << " ";
// 			cout << endl;
// 		}
// 	}
	
	TCell root = bigTable[0][tokenlist.size()];
	for (int i=0; i<root.size(); ++i)
		if (root[i].type == INSTR)
			return true;



	return false;
}

bool parse(string line) {
	line += " ";
	vector<TToken> tokenlist;
	int state = S_INIT;
	int start = 0;

	int length = line.length();
	for (int i=0; i<length; ++i) {
// 		cout << "state: " << state << " ch: " << line[i] << endl;
		switch (state) {
		case S_INIT:
			if (isdigit(line[i])) {
				state = S_NNN;
				start = i;
			}
			else if (line[i] == '"') {
				state = S_SIGN;
				start = i;
				if (i+1<length && line[i+1]==' ') return false;
			}
			else if (isCh(line[i])) {
				state = S_KEYWORD;
				start = i;
			}
			break;
		case S_KEYWORD:
			if (isCh(line[i])) {
			}
			else if (line[i] == ' ') {
				TToken token;
				token.type = KEYWORD;
				token.val = line.substr(start, i-start);
				tokenlist.push_back(token);
				state = S_INIT;
			}
			else return false;
			break;
		case S_SIGN:
			if (line[i] == '"') {
				TToken token;
				token.type = SIGN;
				if (i-1 == start) return false;
				if (line[i-1] == ' ') return false;
				token.val = line.substr(start, i-start+1);
				stringstream ss (stringstream::in|stringstream::out);
				ss << token.val;
				string strBuf;
				token.val = "";
				while (true) {
					strBuf = "";
					ss >> strBuf;
					if (ss.fail()) break;
					if (strBuf.length() == 0) break;
					if (token.val.length() > 0) token.val += " ";
					token.val += strBuf;
				}
				tokenlist.push_back(token);
				state = S_INIT;
			}
			else if (line[i]=='.' && line[i-1]==' ') return false;
			else if (isLetter(line[i]) || line[i]==' ') {
			}
			else return false;
			break;
		case S_NNN:
			if (isdigit(line[i])) {
			}
			else if (line[i] == ' ') {
				TToken token;
				token.type = NNN;
				token.val = line.substr(start, i-start);
				tokenlist.push_back(token);
				state = S_INIT;
			}
			else return false;
			break;
		}
	}
	
	if (state != S_INIT) return false;

// 	for (int i=0; i<tokenlist.size(); ++i)
// 		cout << tokenlist[i].type << " " << tokenlist[i].val << endl;

	originalList = tokenlist;

	return eval(tokenlist);
}

int mainxxxxxxxx(void)
{
	int testcase = 0;
	while (true) {
		memset(line, 0, 100);
		cin.getline(line, 100);
		if (line[0] == '#') break;
		++testcase;
		cout << setw(3) << testcase << ". ";
		if (parse(line) == false)
			cout << "Trap!" << endl;
		else {
			for (int i=0; i<originalList.size(); ++i) {
				cout << originalList[i].val << " ";
			}
			cout << endl;
		}
			
	}
	return 0;
}
